package com.webex.ta.hydra.gui.action;

import com.webex.ta.hydra.core.Command;
import com.webex.ta.hydra.gui.GuiPackage;
import com.webex.ta.hydra.gui.table.HydraTableModel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-26
 * Time: 12:28:30
 */
public class InsertAction implements ActionHandler {
    private static Set<String> commands = new HashSet<String>();

    static {
        commands.add(ActionNames.INSERT_AFTER);
        commands.add(ActionNames.INSERT_BEFORE);
        commands.add(ActionNames.INSERT_EMTPY);
        commands.add(ActionNames.INSERT);
        commands.add(ActionNames.ADD_CHILD);
    }

    public void doAction(ActionEvent e) {
        String keyword = ((JComponent) e.getSource()).getName();
        if (ActionNames.INSERT_AFTER.equals(e.getActionCommand())) {
            insertAfter(keyword);
        } else if (ActionNames.INSERT_BEFORE.equals(e.getActionCommand())) {
            insertBefore(keyword);
        } else if (ActionNames.INSERT.equals(e.getActionCommand())) {
            insert(keyword);
        } else if (ActionNames.ADD_CHILD.equals(e.getActionCommand())) {
            addChild(keyword);
        } else if (ActionNames.INSERT_EMTPY.equals(e.getActionCommand())) {
            insert("");
        }
    }

    private void insert(String keyword) {
        Command cmd = new Command();
        cmd.setKeyword(keyword);
        append(cmd);
//        GuiPackage gp = GuiPackage.getInstance();
//        HydraTableModel model = gp.getHydraTable().getModel();
//        model.appendCommand(model.getRoot(), cmd);
//        gp.getHydraTable().getModel().fireTableDataChanged();
    }


    private void insertAfter(String keyword) {
        GuiPackage gp = GuiPackage.getInstance();
        int row = gp.getHydraTable().getSelectedRow();
        Command cmd = new Command();
        cmd.setKeyword(keyword);
        insert(cmd, row, true);
        gp.getHydraTable().getModel().fireTableDataChanged();
    }

    private void insertBefore(String keyword) {
        GuiPackage gp = GuiPackage.getInstance();
        int row = gp.getHydraTable().getSelectedRow();
        Command cmd = new Command();
        cmd.setKeyword(keyword);
        insert(cmd, row, false);
        gp.getHydraTable().getModel().fireTableDataChanged();
    }

    public void addChild(String keyword) {
        GuiPackage gp = GuiPackage.getInstance();
        int row = gp.getHydraTable().getSelectedRow();
        if (row >= 0) {
            HydraTableModel model = gp.getHydraTable().getModel();
            Command current = model.getCommandAt(row);
            Command cmd = new Command();
            cmd.setKeyword(keyword);
            model.appendCommand(current, cmd);
            model.expand(current);
            model.fireTableDataChanged();
        }
    }



    public static void appendChildTo(Command child, Command parent) {
        HydraTableModel model = GuiPackage.getInstance().getHydraTable().getModel();
        model.appendCommand(parent, child);
        model.expand(parent);
        model.fireTableDataChanged();
    }

    public static void appendChildrenTo(Command children[], Command parent) {
        HydraTableModel model = GuiPackage.getInstance().getHydraTable().getModel();
        model.insertCommands(parent, children, 0);
        model.expand(parent);
        model.fireTableDataChanged();
    }

    public static void append(Command cmd) {
        GuiPackage gp = GuiPackage.getInstance();
        HydraTableModel model = gp.getHydraTable().getModel();
        model.appendCommand(model.getRoot(), cmd);
        gp.getHydraTable().getModel().fireTableDataChanged();
    }

    public static void insert(Command cmd, int row, boolean after) {
        HydraTableModel model = GuiPackage.getInstance().getHydraTable().getModel();
        Command current = model.getCommandAt(row);
        Command parent = current.getParent();
        if (parent != null) {
            int idx = parent.getChildIndexOf(current);
            if (after) {
                model.insertCommand(parent, cmd, idx + 1);
            } else {
                model.insertCommand(parent, cmd, idx);
            }
        }
    }

    public static void insert(Command commands[], int row, boolean after) {
        HydraTableModel model = GuiPackage.getInstance().getHydraTable().getModel();
        Command current = model.getCommandAt(row);
        Command parent = current.getParent();
        if (parent != null) {
            int idx = parent.getChildIndexOf(current);
            if (after) {
                model.insertCommands(parent, commands, idx + 1);
            } else {
                model.insertCommands(parent, commands, idx);
            }
        }
    }

    public Set<String> getActionNames() {
        return commands;
    }
}
